package com.handong.handserialprogram.module.cofigring.config;

import android.os.Handler;
import android.widget.Toast;

import com.handong.handserialprogram.model.entities.FootRingNewEntity;
import com.handong.handserialprogram.model.status.RingResponse;
import com.handong.handserialprogram.module.base.BasePresenter;
import com.handong.handserialprogram.module.base.BaseView;
import com.handong.handserialprogram.module.guide.GuidePresenter;
import com.handong.handserialprogram.module.serialport.SerialPortUtil;
import com.handong.handserialprogram.utils.EventBusUtil;
import com.handong.handserialprogram.utils.JLog;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by userKey on 2018/6/22.
 */

public class ConfigPresenter extends BasePresenter<ConfigActivity> {
    protected static final String TAG = GuidePresenter.class.getSimpleName();
    private Handler mHandler=new Handler();

    public ConfigPresenter(BaseView view) {
        super(view);
        EventBusUtil.register(this);//EventBus
    }
    @Override
    protected void exit() {
        EventBusUtil.unRegister(this);
        super.exit();
        JLog.d(TAG, "Active Data:" + "exit");
    }

    public void setFootRingAlarm(String id,int code){
        mView.showLoadDialog("正在设置警报声音...");
        SerialPortUtil.getInstance(mView).setFootRingAlarm(id,String.valueOf(code));
    }

    private void setFootRingIP(String id){
        SerialPortUtil.getInstance(mView).setFootRingIP(id,mView.getIpAdressData());
    }
    private void endConfig(String id){
        SerialPortUtil.getInstance(mView).endConfig(id);
    }

    private void dealException(String message){
        Toast.makeText(mView,message,Toast.LENGTH_SHORT).show();
        mView.dismissLoadingDialog();
    }

    /**
     * 接收脚环反馈
     * @param response
     */
    @Subscribe(priority = 100, threadMode = ThreadMode.MAIN)
    public void receiveRingResponse(RingResponse response) {
        switch (response.getResponseCode()) {
            case RingResponse.ALARM_SOUND_SET://警报设置成功
                if(response.isSuccess()){
                    mView.showLoadDialog("正在设置脚环IP地址...");
                    setFootRingIP(response.getRingID());
                }else dealException("设置警报声音失败");

                break;
            case RingResponse.SEVER_IP_SET://ip设置成功
                if(response.isSuccess()){
                    mView.showLoadDialog("配置成功，正在退出配置模式...");
                    endConfig(response.getRingID());
                }else dealException("设置脚环IP失败");

                break;
            case RingResponse.PARAMERT_LOGOUT://退出配置模式成功
                if(response.isSuccess()){
                    Toast.makeText(mView,"配置成功",Toast.LENGTH_SHORT).show();
                    mView.dismissLoadingDialog();
                    mView.finish();
                }else dealException("退出配置模式失败");
                break;
        }
    }

    /**
     * 检查脚环数据是否初始化完成
     */
    public void checkFootRingData(final FootRingNewEntity entity){
        mView.showLoadDialog("正在加载脚环数据");
        final Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            private int count=0;
            @Override
            public void run() {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if(entity.isNormalWork()){//初始化完成
                            mView.setIpDefault();
                            mView.setAlarmDefault();
                            mView.dismissLoadingDialog();
                            timer.cancel();
                            return;
                        }else{
                            count++;
                            if(count==25){//超时
                                mView.dismissLoadingDialog();
                                timer.cancel();
                                mView.showLongToast("获取脚环数据失败");
                                mView.finish();
                            }
                        }
                    }
                });
            }
        },0,200);
    }

}
